package gatperdut.chato.core.interpreter.actions;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.constants.CSConstantsInventory;
import gatperdut.chato.constants.CSConstantsString.Possessive;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.interpreter.actions.generic.Action;
import gatperdut.chato.core.interpreter.grammar.comminfo.WearInfo;
import gatperdut.chato.core.item.ItemController;
import gatperdut.chato.core.item.inventory.EntityInventoryController;
import gatperdut.chato.model.Chato.Constant.WearLoc;

public class WearAction extends Action {

    public WearAction( WearInfo info, BaseController entityController ) {
        super( info, entityController );
    }

    @Override
    public CommandExecutionResult perform() {
        checkForRunningCraft();

        if ( emptyEmote() ) {
            getEntityController().tx( "Empty emotes are not allowed." );
            return CommandExecutionResult.OK;
        }

        String emote = getLastPartEmote();
        if ( emote == null ) {
            getEntityController().tx( "Check your emote." );
            return CommandExecutionResult.OK;
        }

        if ( !checkUnindexedWord( getInfo().getWhat() ) ) {
            return CommandExecutionResult.OK;
        }
        ItemController ic = getEntityController().getInventoryController().getHeldItemController( getInfo().getWhat() );
        if ( ic == null ) {
            getEntityController().tx( "You don't seem to be holding that." );
            return CommandExecutionResult.OK;
        }

        if ( !ic.isWearable() ) {
            getEntityController().tx( ic.getShortDesc() + " cannot be worn." );
            return CommandExecutionResult.OK;
        }

        WearLoc wearLoc;
        if ( getInfo().getWearLoc() != null ) {
            wearLoc = WearLoc.get( getInfo().getWearLoc().toUpperCase() );
            if ( wearLoc == null ) {
                getEntityController().tx( "I don't recognize that wear location." );
                return CommandExecutionResult.OK;
            }
        }
        else {
            wearLoc = ic.getFirstWearLoc();
        }

        EntityInventoryController eic = getEntityController().getInventoryController();

        if ( !ic.acceptsWearLoc( wearLoc ) ) {
            getEntityController().tx( ic.getShortDesc() + " cannot be worn there." );
            return CommandExecutionResult.OK;
        }

        if ( !eic.isWearLocFree( wearLoc ) ) {
            getEntityController().tx(
                    "You are already wearing something "
                            + CSConstantsInventory.getWearLocPossessiveInvName( wearLoc ).replace( "@", Possessive.YOUR.getValue() ) + "." );
            return CommandExecutionResult.OK;
        }

        eic.takeFromInventory( ic );

        eic.putInInventory( wearLoc, ic );

        getEntityController().getEchoes().txWear( ic, wearLoc, emote );

        return CommandExecutionResult.OK;
    }

    @Override
    public WearInfo getInfo() {
        return (WearInfo) super.getInfo();
    }

    @Override
    public boolean interruptsCrafting() {
        return false;
    }

}
